package com.situ.shoplook.admin.api;

import com.github.pagehelper.PageInfo;
import com.situ.shoplook.common.model.Order;
import com.situ.shoplook.common.model.Result;
import com.situ.shoplook.common.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/v1/order", produces = "application/json;charset=utf-8")
public class OrderApi {

    private OrderService orderService;

    @Autowired
    public void setAdminService(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * add
     */
    @PostMapping
    public Result add(@RequestBody Order order) {
        try {
            orderService.save(order);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * remove
     */
    @DeleteMapping
    public Result remove(@RequestBody Integer[] ids) {
        orderService.remove(ids);
        return Result.success();
    }
    /**
     * update
     */
    @PutMapping
    public Result update(@RequestBody Order order) {
        try {
            orderService.update(order);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * findAll
     */
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "1",value = "page") Integer page,
                          @RequestParam(defaultValue = "10",value = "limit") Integer limit, Order order) {
        try {
            PageInfo<Order> pageInfos = orderService.searchByPage(page, limit, order);
            return Result.success(pageInfos);
        }catch (Exception e){
            return Result.error("无查询结果");
        }

    }
}
